/**
 * Coryright 2015-2020 www.eyaomall.cn
 * All rights reserved.
 * @author huwenqing
 * @date 2016年8月3日
 *
 */
package com.eyaomall.wxmanage.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eyaomall.wxmanage.common.error.BECode;
import com.eyaomall.wxmanage.model.po.Article;
import com.eyaomall.wxmanage.service.ArticleService;
import com.github.pagehelper.StringUtil;
import com.lycheeframework.core.cmp.api.http.GsnData;
import com.lycheeframework.core.cmp.api.http.GsnResponse;
import com.lycheeframework.core.cmp.kit.PageHTTPObject;
import com.lycheeframework.core.exception.ErrorCodeException;

/**
 * 文章库
 * @author Administrator
 *
 */
@Controller
@RequestMapping("article")
public class ArticleController {
	
	@Autowired
	private ArticleService articleService;
	
	/**
	 * 首页
	 */
	@RequestMapping("index")
	public String index(){
		return "article/index";
	}
	
	/**
	 * 编辑文章页面
	 */
	@RequestMapping("editor_page")
	public String editorPage(){
		return "article/editor";
	}
	
	/**
	 * 编辑页面
	 */
	@RequestMapping("editor")
	public String editorPage(Integer id,Map<String, Object> model){
		Article article = new Article();
		article.setId(id);
		model.put("article", articleService.query(article));
		return "article/editorPage";
	}
	
	/**
	 * 编辑更新
	 */
	@RequestMapping("update")
	public @ResponseBody GsnData updateData(Article atrical){
		validate(atrical);
		
		return GsnResponse.toData(articleService.updateData(atrical));
	}
	
	
	/**
	 * 分页查询数据
	 */
	@RequestMapping("page_data")
	public @ResponseBody GsnData pageData(Article article,PageHTTPObject object){
		return GsnResponse.toData(articleService.pageDate(article,object));
	}
	
	/**
	 * 保存编辑
	 */
	@RequestMapping("save")
	public @ResponseBody GsnData saveData(Article atrical){
		validate(atrical);
		
		return GsnResponse.toData(articleService.save(atrical));
	}
	
	/**
	 * 根据ID删除
	 * @param id
	 * @return
	 */
	@RequestMapping("del")
	public @ResponseBody GsnData del(Integer id){
		
		return GsnResponse.toData(articleService.del(id));
	}
	
	/**
	 * 校验文章库数据
	 * @param article
	 */
	private void validate(Article article){
		if(StringUtil.isEmpty(article.getTitle()))
			throw new ErrorCodeException(BECode.article_600000);
		
		if(article.getTitle().length() > 60)
			throw new ErrorCodeException(BECode.article_600001);
		
		if(StringUtil.isNotEmpty(article.getAuthor()) && article.getAuthor().length() > 15)
			throw new ErrorCodeException(BECode.article_600002);
		
		if(StringUtil.isNotEmpty(article.getSource()) && article.getSource().length() > 15)
			throw new ErrorCodeException(BECode.article_600003);
		
		if(StringUtil.isNotEmpty(article.getRemark()) && article.getRemark().length() > 64)
			throw new ErrorCodeException(BECode.article_600004);
		
		if(StringUtil.isNotEmpty(article.getHeadImg()) && article.getHeadImg().length() > 128)
			throw new ErrorCodeException(BECode.article_600005);
		
		if(StringUtil.isEmpty(article.getContent()) || article.getContent().length() > 20000)
			throw new ErrorCodeException(BECode.article_600006);
	}
}
